#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myTransceiver.h"

#include <QMessageBox>
#include <QTextCodec>
#include <QMouseEvent>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("");

    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB18030"));

    display_rtx = new MyTransceiver();

    // when new data comes, just show it in textBrowser
    connect(display_rtx,SIGNAL(newData()),this,SLOT(showData()));
}

void MainWindow::showData()
{
    ui->showData->insertPlainText(display_rtx->buffer);
}

MainWindow::~MainWindow()
{
    delete display_rtx;
    delete ui;
}

void MainWindow::on_pushButton_1_clicked()
{
    display_rtx->sendData();
}

void MainWindow::on_pushButton_2_clicked()
{
//    closeSerialPort();
}

void MainWindow::on_comboBox_1_currentIndexChanged(int index)
{
    switch(index)
    {
        case 0: ui->statusBar->showMessage(tr("Hello, comboBox!"));
            break;
        case 1: ui->statusBar->showMessage(tr("Bye, comboBox!"));
            break;
        case 2: ui->statusBar->showMessage(tr("Nice job!"));
            break;
        default: ui->statusBar->showMessage(tr("Invalid item!"));
            break;
    }
}

void MainWindow::on_comboBox_2_currentIndexChanged(int index)
{
    switch(index)
    {
        case 0: ui->statusBar->showMessage(tr("Hello, comboBox!"));
            break;
        case 1: ui->statusBar->showMessage(tr("Bye, comboBox!"));
            break;
        case 2: ui->statusBar->showMessage(tr("Nice job!"));
            break;
        default: ui->statusBar->showMessage(tr("Invalid item!"));
            break;
    }
}

void MainWindow::on_comboBox_3_currentIndexChanged(int index)
{
    switch(index)
    {
        case 0: ui->statusBar->showMessage(tr("Hello, comboBox!"));
            break;
        case 1: ui->statusBar->showMessage(tr("Bye, comboBox!"));
            break;
        case 2: ui->statusBar->showMessage(tr("Nice job!"));
            break;
        default: ui->statusBar->showMessage(tr("Invalid item!"));
            break;
    }
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->showData->clear();
}


void MainWindow::mouseDoubleClickEvent(QMouseEvent *e)
{
    if (e->button()==Qt::LeftButton)
    {
        ui->showData->clear();
    }
}

